// JavaScript Document
function populateDestinos(idHotel,Destinos,destinyList){
		ClearList(destinyList);
		for(z=0;z<Destinos.length;z++){
				//alert(Destinos[z][0]+"=="+idHotel);
				if(Destinos[z][0]==idHotel){
					AddToList(destinyList,Destinos[z][1],Destinos[z][2]);
				}
		}
}

function ClearList(List){
	if(List.length>0){
		for(x=List.length;x>=0;x=x-1){
			List[x]= null;	
		}
	}
}

function AddToList(List,Value,Text){
	List[List.length]= new Option(Text,Value);
}

function aparece(id){
	alert();
	if(document.getElementById(''+id).style.diaplay=='none'){
		document.getElementById(''+id).style.display='';
	}
	else{
		document.getElementById(''+id).style.display='none';
	}
}

//takes a document.form as argument, the fields as an array and the name of the fields as an array
function checkRequiredFields(f,fields,names){
	for (var i = 0; i < f.length; i++) {
	   	var e = f.elements[i];
		var pos=inArray(fields,e.name);
	   	if(pos!=-1){
			if ((e.type == 'file')||(e.type == 'text')||(e.type == 'textarea')||(e.type == 'password')) {
		    	if(e.value==""||e.value==null){
		    		alert(names[pos]+" es un campo obligatorio.");
		    		e.focus();
		    		return false;
		    	}	
		  	}
		    else if ((e.type == 'select-one')) {
		    	if(e.options[e.selectedIndex].value==""){
		    		alert(names[pos]+" es un campo obligatorio.");
		    		e.focus();
		    		return false;
		    	}	 	
		  	}
		    else if ((e.type == 'select-multiple')) {
		    	if(e.selectedIndex==-1){
		    		alert(names[pos]+" es un campo obligatorio.");
		    		e.focus();
		    		return false;
		    	}	 	
		  	}
		}
	}
	return true;
}
//takes document.myForm.field as arguement and returns true if it is a number; false otherwise
function isANumber(field,acceptNegative){
	for(var i=0;i<field.value.length;i++){
		check=field.value.substring(i,i+1);
		if ((i==field.value.length-3)||(i==field.value.length-2)){
			if(check.match("[^0-9.]")!=null){
				field.select();
				return false;
				}
		}
		else if (i==0){
			if(acceptNegative){
				if(check.match("[^0-9-]")!=null){
					field.select();
					return false;
				}
			}
			else{
				if(check.match("[^0-9]")!=null){
					field.select();
					return false;
				}
			}
		}
		else if(i==field.value.length-1){
			if(check.match("[%]")!=null){
				field.select();
				return false;
			}
		}
		else if(check.match("[^0-9]")!=null){
			field.select();
			return false;
		}
	}
	return true;
}

//takes docuement.myForm.field as arguement and converts the date to mm/dd/yyyy if valid otherwise; returns error
function isValidDate(field){
	var err=0
	var psj=0;
	a=field.value
	if(a=="")
		return true;
	if (a.length != 10) err=1
	b = a.substring(0, 2)// month
	c = a.substring(2, 3)// '/'
	d = a.substring(3, 5)// day
	e = a.substring(5, 6)// '/'
	f = a.substring(8, 10)// year

	//basic error checking
	if (b<1 || b>12) err = 1
	if (c != '/') err = 1
	if (d<1 || d>31) err = 1
	if (e != '/') err = 1
	if (f<0 || f>99) err = 1
	
	//advanced error checking

	// months with 30 days
	if (b==4 || b==6 || b==9 || b==11){
		if (d==31) err=1
	}

	// february, leap year
	if (b==2){
		// feb
		var g=parseInt(f/4)
		if (isNaN(g)) {
			err=1
		}

		if (d>29) err=1
		if (d==29 && ((f%4)!=0)) err=1
	}
	if (err==1){
		alert("El valor " + a + " no es una fecha valida.\n\r" +  
			"Por favor ingrsa la fecha en el formato mm/dd/yyyy");
	    field.select()
		return false;
	}
	else{
		return true;
	}

}
//regrea -1 si el valor encontrado no esta en el arreglo o la posicion si lo encuentra
function inArray (arr,value)
{
    var i;
    for (i=0; i < arr.length; i++) {
        // Matches identical (===), not just similar (==).
        if (arr[i] === value) {
            return i;
        }
    }
    return -1;
};


/**
*	Funcion unicamente para ocultar un mensaje...
*/
ocultarMensajeFlotante = function(obj){		
	obj.style.display = "none";
}

/**
*	Funcion para poner la fecha inicial a un componente de entrada de fecha...
*/
function setFechaActual(campo){
	fechaActual = new Date();
	var dia = fechaActual.getDate();
	switch (fechaActual.getDate()){
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
		case 6:
		case 7:
		case 8:
		case 9:
			dia = "0" + fechaActual.getDate();
			break;
	}		
	var mes = fechaActual.getMonth()+1;
	switch (fechaActual.getMonth()+1){
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
		case 6:
		case 7:
		case 8:
		case 9:
			mes = "0" + (fechaActual.getMonth()+1);
			break;
	}				
//		alert(fechaActual.getDate()+'/'+(fechaActual.getMonth()+1)+'/'+fechaActual.getFullYear());
	campo.value = (dia +'/'+mes+'/'+ fechaActual.getFullYear());			
}
